﻿using System;
using System.Text.RegularExpressions;

namespace Hsenl {
    public static class PathHelper {
        public static string GetFolderPath(string path, char split = '/') {
            return path[..path.LastIndexOf(split)];
        }

        public static string GetLocalPath(string path, string relativePath) {
            if (string.IsNullOrEmpty(path))
                throw new ArgumentNullException(nameof(path));

            return path.Replace(relativePath, "");
        }

        public static bool IsValidFileNameOrFolderName(string name) {
            // 正则表达式匹配非法字符
            var pattern = @"[<>:""/\\|?*]";
            var regex = new Regex(pattern);

            // 检查是否包含非法字符
            if (regex.IsMatch(name)) {
                return false;
            }

            // 检查是否为空或仅包含空格
            if (string.IsNullOrWhiteSpace(name)) {
                return false;
            }

            // 检查是否以点（.）开头或结尾
            if (name.StartsWith(".") || name.EndsWith(".")) {
                return false;
            }

            // 检查长度是否超过系统限制(通常为260个字符)
            if (name.Length > 250) {
                return false;
            }

            return true;
        }
    }
}